package com.pateo.bjev.bjevvehicle.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TextView;

import com.pateo.bjev.bjevvehicle.R;
import com.pateo.bjev.bjevvehicle.ui.widget.TabLayout;

/**
 * Created by pateo on 17-6-1.
 */

public class PowerCostTabView extends TabLayout {
    public PowerCostTabView(Context context) {
        super(context);
    }

    public PowerCostTabView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void addTab(Tab tab) {
        TextView textView = new TextView(getContext());
        textView.setText(tab.title);
        textView.setPadding(10,10,10,10);
        textView.setGravity(Gravity.CENTER);
        textView.setOnClickListener(this);

        textView.setTextSize(getResources().getDimensionPixelSize(R.dimen.tab_text_size));
        if (isFirstAdd()) {
            textView.setBackgroundColor(getResources().getColor(R.color.tab_selected_textcolor));
            textView.setTextColor(Color.BLACK);
        } else {
            textView.setBackgroundColor(getResources().getColor(R.color.tab_unselected_textcolor));
            textView.setTextColor(Color.WHITE);
        }

        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        params.gravity = Gravity.CENTER_VERTICAL;

        addView(textView, params);
    }

    public void selectPosition(int position) {
        selectedIndex = position;
        TextView selectView = (TextView) getChildAt(selectedIndex);
//        selectView.setTextColor(Color.BLACK);
//        selectView.setBackground(R.color.custom_tab_selected_color);
        for (int i = 0; i < getChildCount(); i++) {
            TextView t = (TextView) getChildAt(i);
            if (i != selectedIndex) {
                t.setBackgroundColor(getResources().getColor(R.color.custom_tab_unselected_color));
                t.setTextColor(Color.WHITE);
            }
        }
        postInvalidate();
        notifyTabSelectedChanged(position);
    }

    @Override
    protected void onDraw(Canvas canvas) {

    }
}
